﻿using System;
using System.Configuration;
using System.Text;
using T.Rest;

namespace T.REST
{
    public class RessourceFactory
    {
        private static Ressource _asserts;

        /// <summary>
        /// Initializes the factory which creates Ressource objects for the test framework.
        /// </summary>
        public static void Init(Action<string> assertFail, Action<Type, Type, string> assertAreEqualTypes, Action<int, int, string> assertAreNotEqualInts, Action<int, int, string> assertAreEqualInts, Action<bool, bool, string> assertAreNotEqualBooleans)
        {
            _asserts = new Ressource(assertFail, assertAreEqualTypes, assertAreNotEqualInts, assertAreEqualInts, assertAreNotEqualBooleans);
        }

        /// <summary>
        /// Initializes and returns a Ressource object with the desired configuration
        /// </summary>
        public static Ressource Create(RestConfiguration config)            
        {
            if (_asserts == null)
                throw new ArgumentException("Factory not initialized");

            if (_asserts.AssertFail == null)
                throw new ArgumentException("Factory not yet initialized - assertFail is null");
            if (_asserts.AssertAreEqualTypes == null)
                throw new ArgumentException("Factory not yet initialized - assertAreEqual is null");            
            if (_asserts.AssertAreNotEqualInts == null)
                throw new ArgumentException("Factory not yet initialized - assertAreNotEqual is null");
            if (_asserts.AssertAreEqualInts == null)
                throw new ArgumentException("Factory not yet initialized - assertAreNotEqual is null");
            if (_asserts.AssertAreNotEqualBoolean == null)
                throw new ArgumentException("Factory not yet initialized - assertAreNotEqualBoolean is null"); 

            
            if (string.IsNullOrEmpty(config.Url))
                throw new ArgumentException("Url is empty");

            var obj = new Ressource(_asserts.AssertFail, _asserts.AssertAreEqualTypes, _asserts.AssertAreNotEqualInts, _asserts.AssertAreEqualInts, _asserts.AssertAreNotEqualBoolean)
                {
                    Configuration = config
                };

            return obj;
        }
    }
}
